package bb.lanxing.device.sgdevcie.storage.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import bb.lanxing.R;
import bb.lanxing.databinding.FragmentXossSGDeviceRoutebookManagerBinding;
import bb.lanxing.device.sgdevcie.storage.adapter.XossDeviceRouteBookManageAdapter;
import bb.lanxing.device.sgdevcie.viewmodel.DeviceViewModel;
import bb.lanxing.lib.devices.sgsettingentity.routebook.Route;
import bb.lanxing.mvvm.base.BaseDBFragment;
//import bb.lanxing.mvvm.base.VMStoreKt$viewModelWithScope$2;
//import bb.lanxing.mvvm.base.VMStoreKt$viewModelWithScope$3;
import java.util.List;

import kotlin.jvm.internal.Intrinsics;
/* compiled from: XossSGDeviceRouteBookManagerFragment.kt */

public final class XossSGDeviceRouteBookManagerFragment extends
        BaseDBFragment<FragmentXossSGDeviceRoutebookManagerBinding> {
    private boolean isEdit;
    private Menu menu;
    private XossDeviceRouteBookManageAdapter xossDeviceRouteBookManageAdapter;
//    private final Lazy xossSGDeviceViewModel$delegate;//TODO:hu

    public XossSGDeviceRouteBookManagerFragment() {
        super(R.layout.fragment_xoss_s_g_device_routebook_manager);
        final XossDeviceRouteBookManageAdapter routeBookAdapter = new XossDeviceRouteBookManageAdapter();
        routeBookAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public final boolean onItemLongClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                PopupMenu popupMenu = new PopupMenu(requireContext(), view, Gravity.BOTTOM);
                popupMenu.inflate(R.menu.menu_delete);
                popupMenu.setOnMenuItemClickListener(menuItem -> {
                    routeBookAdapter.clearChecked();
                    if (menuItem.getItemId() == R.id.delete) {
                        routeBookAdapter.getItem(i).setChecked(true);
                        deleteChecked();
                    }
                    return true;
                });
                popupMenu.show();
                return true;

            }
        });
        this.xossDeviceRouteBookManageAdapter = routeBookAdapter;
        XossSGDeviceRouteBookManagerFragment xossSGDeviceRouteBookManagerFragment = this;
        //TODO:hu
//        this.xossSGDeviceViewModel$delegate = FragmentViewModelLazyKt.createViewModelLazy(xossSGDeviceRouteBookManagerFragment, Reflection.getOrCreateKotlinClass(XossSGDeviceViewModel.class), new VMStoreKt$viewModelWithScope$2(xossSGDeviceRouteBookManagerFragment, "SCOPE_STORAGE_MANAGE"), new VMStoreKt$viewModelWithScope$3(xossSGDeviceRouteBookManagerFragment), null);
    }

    public DeviceViewModel getXossSGDeviceViewModel() {
        //TODO:hu
//        return (XossSGDeviceViewModel) this.xossSGDeviceViewModel$delegate.getValue();
        return null;
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        XossSGDeviceRouteBookManagerFragment xossSGDeviceRouteBookManagerFragment = this;
        //TODO:hu
//        LifecycleOwnerKt.getLifecycleScope(xossSGDeviceRouteBookManagerFragment).launchWhenResumed(new XossSGDeviceRouteBookManagerFragment$onCreate$1(this, null));
//        LifecycleOwnerKt.getLifecycleScope(xossSGDeviceRouteBookManagerFragment).launchWhenResumed(new XossSGDeviceRouteBookManagerFragment$onCreate$2(this, null));
    }

    @Override
    public void initView(FragmentXossSGDeviceRoutebookManagerBinding binding) {
        setHasOptionsMenu(true);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        RecyclerView recyclerView = binding.recyclerView;
        DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
        defaultItemAnimator.setSupportsChangeAnimations(false);
        recyclerView.setItemAnimator(defaultItemAnimator);
        binding.recyclerView.setAdapter(this.xossDeviceRouteBookManageAdapter);
        binding.radioGroup.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == R.id.radio_all) {
                xossDeviceRouteBookManageAdapter.checkAll();
            }
        });
        binding.tvDelete.setOnClickListener(view -> deleteChecked());
    }

    public void deleteChecked() {
        //TODO:hu
//        getXossSGDeviceViewModel().deleteDeletableList(this.xossDeviceRouteBookManageAdapter.getCheckedItem(), new XossSGDeviceRouteBookManagerFragment$deleteChecked$1(this), new XossSGDeviceRouteBookManagerFragment$deleteChecked$2(this), new XossSGDeviceRouteBookManagerFragment$deleteChecked$3(this));
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        if (!this.isEdit) {
            inflater.inflate(R.menu.menu_edit, menu);
        } else {
            inflater.inflate(R.menu.menu_done, menu);
        }
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        Intrinsics.checkNotNullParameter(menu, "menu");
        super.onPrepareOptionsMenu(menu);
        this.menu = menu;
        if (!this.isEdit) {
            List<Route> data = this.xossDeviceRouteBookManageAdapter.getData();
            if (data.isEmpty()) {
                dismissOptionMenu();
            }
        }
    }

    public final void dismissOptionMenu() {
        Menu menu = this.menu;
        if (menu != null) {
            menu.getItem(0).setVisible(false);
            menu.getItem(0).setEnabled(false);
        }
    }

    public final void showOptionMenu() {
        Menu menu = this.menu;
        if (menu != null) {
            menu.getItem(0).setVisible(true);
            menu.getItem(0).setEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        FragmentXossSGDeviceRoutebookManagerBinding binding = getBinding();
        if (item.getItemId() == R.id.menu_item_done) {
            this.isEdit = false;
            requireActivity().invalidateOptionsMenu();
            binding.llBottomPannel.setVisibility(View.GONE);
            this.xossDeviceRouteBookManageAdapter.setEditMode(false);

        } else if (item.getItemId() == R.id.menu_item_edit) {
            this.isEdit = true;
            requireActivity().invalidateOptionsMenu();
            binding.llBottomPannel.setVisibility(View.VISIBLE);
            binding.radioGroup.clearCheck();
            this.xossDeviceRouteBookManageAdapter.setEditMode(true);
        }
        return true;
    }
}
